package com.liaofan.rocketmq.retry;



import com.liaofan.rocketmq.util.StaticBianLiang;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * 同步发送消息
 */

public class SyncProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("pg");
        producer.setNamesrvAddr(StaticBianLiang.NameServerAddress);
        producer.setSendMsgTimeout(3000);
        producer.setRetryTimesWhenSendFailed(5);
        producer.start();

        for (int i = 0; i < 100; i++) {
            byte[] body = ("Hi,Liaofan" + i).getBytes();
            Message msg = new Message("someTopic_retry", "someTag_retry", body);
            msg.setKeys("Just Do it:" + i);
            SendResult sendResult = producer.send(msg);
            System.out.println(sendResult);
        }

        producer.shutdown();
    }
}
